/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.controller.ajax;

import net.groupme.controller.model.GroupMeSearchResult;
import net.groupme.controller.model.HtmlFormattedElement;
import net.groupme.controller.model.ImageSearchResult;
import net.groupme.controller.model.SearchResult;
import net.groupme.gui.constants.VIEW_VOCABULARY;
import net.groupme.model.MODEL_CONSTANTS;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * created on Jun 26, 2007
 * 
 * This controller is executed whenever a result of a Search (which can be an arbitrary resource)
 * should be visualized in the Search Reasult list. This task is decoupled from the actual View (jsp)
 * in order to allow different visualizations for different (various) resource types (e.g. photos, 
 * websites,...).
 * 
 * @author Fabian Abel, <a href="mailto:abel@l3s.de">abel@l3s.de</a>
 * @author last edited by: $Author: mischa $
 * 
 * @version $Revision: 1.14 $ $Date: 2008-02-28 15:08:51 $
 */
public class SearchResultRenderingController implements ApplicationContextAware{

	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());
	
	/** the Spring application context */
	private ApplicationContext applicationContext = null;
	
	/* (non-Javadoc)
	 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
	 */
	public void setApplicationContext(ApplicationContext context) throws BeansException {
		this.applicationContext = context;
	}
	
	/**
	 * Formats a given {@link SearchResult} according to its type within {@link HtmlFormattedElement} should be included
	 * a div-tag which has the given ID.
	 * @param id the ID of the div-tag into which the 
	 * @param searchResult the search result to visualize
	 * @return a {@link HtmlFormattedElement} instance which contains  the HTML-formatted string
	 */
	public HtmlFormattedElement getVisualization(String id, SearchResult searchResult){
		logger.info("Trying to visualize: " + searchResult);
		
		ResourceAggregationAjaxController aggregator = (ResourceAggregationAjaxController)applicationContext.getBean(VIEW_VOCABULARY.RESSOURCE_AGGREGATION, ResourceAggregationAjaxController.class);
		SearchResult resourceToRender = aggregator.getSearchResultForID(searchResult.getResultID());
		
		String divSearchResult = null;
		if(resourceToRender instanceof ImageSearchResult){
			divSearchResult = getImageSearchResultVisualization(((ImageSearchResult)resourceToRender).getUrlOfThumbnail());
		}
		else if (MODEL_CONSTANTS.RESOURCE_TYPE_WEBSITE.equals(searchResult.getType()) 
				|| MODEL_CONSTANTS.RESOURCE_TYPE_SHOCKWAVE.equals(searchResult.getType())
				|| MODEL_CONSTANTS.RESOURCE_TYPE_AUDIO.equals(searchResult.getType())
				|| MODEL_CONSTANTS.RESOURCE_TYPE_RSS.equals(searchResult.getType())
				|| MODEL_CONSTANTS.RESOURCE_TYPE_VIDEO.equals(searchResult.getType())){
			divSearchResult = getWebsiteSearchResultVisualization(resourceToRender.getName(), resourceToRender.getDescription(), resourceToRender.getType(), resourceToRender.getUrl());
		}
		else if(resourceToRender instanceof GroupMeSearchResult){
			GroupMeSearchResult groupMeResult = (GroupMeSearchResult)resourceToRender;
			if(MODEL_CONSTANTS.RESOURCE_TYPE_IMAGE.equals(groupMeResult.getResource().getType())){
				divSearchResult = getImageSearchResultVisualization(groupMeResult.getUrl());
			}else {
				divSearchResult = getWebsiteSearchResultVisualization(groupMeResult.getName(), groupMeResult.getDescription(), resourceToRender.getType(), resourceToRender.getUrl());
			}
		}
		else{
			divSearchResult = searchResult.getName();
		}
		return new HtmlFormattedElement(id, divSearchResult);
	}
	
	/**
	 * Formats a given {@link SearchResult} according to its type within {@link HtmlFormattedElement} should be included
	 * a div-tag which has the given ID.
	 * @param id the ID of the div-tag into which the 
	 * @param searchResult the search result to visualize
	 * @return a {@link HtmlFormattedElement} instance which contains  the HTML-formatted string
	 */
	public HtmlFormattedElement getVisualizationForGame(String id, SearchResult searchResult){
		logger.info("Trying to visualize: " + searchResult);
		String divSearchResult = null;
		String smallDescription = "";
		if(searchResult instanceof ImageSearchResult){
			divSearchResult = getImageSearchResultVisualization(((ImageSearchResult)searchResult).getUrlOfThumbnail());
		}
		else if (MODEL_CONSTANTS.RESOURCE_TYPE_WEBSITE.equals(searchResult.getType()) 
				|| MODEL_CONSTANTS.RESOURCE_TYPE_SHOCKWAVE.equals(searchResult.getType())
				|| MODEL_CONSTANTS.RESOURCE_TYPE_AUDIO.equals(searchResult.getType())
				|| MODEL_CONSTANTS.RESOURCE_TYPE_RSS.equals(searchResult.getType())
				|| MODEL_CONSTANTS.RESOURCE_TYPE_VIDEO.equals(searchResult.getType())){
			if (searchResult.getDescription().length() > 62){
				smallDescription = searchResult.getDescription().substring(0, 60)+"...";
			}
			divSearchResult = getWebsiteSearchResultVisualization(searchResult.getName(), smallDescription, searchResult.getType(), searchResult.getUrl());
		}
		else if(searchResult instanceof GroupMeSearchResult){
			GroupMeSearchResult groupMeResult = (GroupMeSearchResult)searchResult;
			if (groupMeResult.getDescription().length() > 62){
				smallDescription = groupMeResult.getDescription().substring(0, 60)+"...";
			}
			if(MODEL_CONSTANTS.RESOURCE_TYPE_IMAGE.equals(groupMeResult.getResource().getType())){
				divSearchResult = getImageSearchResultVisualization(groupMeResult.getUrl());
			}else {
				divSearchResult = getWebsiteSearchResultVisualization(groupMeResult.getName(), smallDescription, searchResult.getType(), searchResult.getUrl());
			}
		}
		else{
			divSearchResult = searchResult.getName();
		}
		return new HtmlFormattedElement(id, divSearchResult);
	}
	
	/**
	 * Returns a HTML-formatted string which renders an image search result.
	 * @param thumbnailURL the URL of the (thumbnail) image to show
	 * @return HTML-formatted string which renders an image search result
	 */
	private String getImageSearchResultVisualization(String thumbnailURL){
		logger.info("Rendering an image search result.");
		return " <p style=\"margin-bottom:4px\"> " +"<img src='" + thumbnailURL + "' width='100%'/>";
	}
	
	/**
	 * Returns a String wicht is trimmed to be max 20 characters long and the first 7 characters are removed "http://"
	 * @param the string to be trimmed
	 * @return the trimmed string with "..." on the end if trimmed
	 */
	private String trimLink(String link){
		link = link.substring(7);
		if (link.length()> 20){
			link =link.trim().substring(0, 20) + "...";
		}
		return link;
	}
	
	/**
	 * Returns a HTML-formatted string which renders a website search result.
	 * @param title the title  of the website search result
	 * @param shortDescription a short appetizer description
	 * @return HTML-formatted string which renders the website search result
	 */
	private String getWebsiteSearchResultVisualization(String title, String shortDescription, String contentType, String link){
		logger.info("Rendering a "+ contentType + " search result.");
		String html = "" 
			+	"<p style=\"margin-bottom:5px\">"
			+		"<u>"
			+ 			"<font size=\"2\" color=\"#006699\">" + title + "</font>"
			+		"</u>"
			+	"</p>"
			+	"<p style=\"margin-bottom:6px\"><font size=\"1\">" + shortDescription + "</font></p>"
			+	"<p style=\"margin-bottom:4px\"><font size=\"1\"> Filetype: " + contentType + "</font></p>"
			+	"<p style=\"margin-bottom:10px\"><font size=\"1\"> URL: " + "<a href=\"" + link + "\" target=\"blank\">" + trimLink(link) +  "</a>" + "</font></p>";
		return html;
	}
}
